<template>
	<view id="domxee-content" :class="globalData.defaultThemes">
		<view id="header_wrapper">
			<nii-navbar :globalData="globalData" :scroll="scroll" back>
				<view slot="title">
					<view id="title" class="fw-bolder">我的订单</view>
				</view>
			</nii-navbar>
		</view>
		<view id="content_wrapper">
			<view id="search" v-if="renders.search.enable">
				<view class="search d-padding">
					<view class="search_box border-color page_bg d-flex align-items-center">
						<view class="domxee_icon search_icon color fw-bold">&#xe615;</view>
						<input type="text" class="main_text fw-bold search_input w-fill-available" v-model="data.sections.formData.search_keyword" placeholder-class="vice_text" :placeholder="renders.search.placeholder">
						<view class="search_button bg text-nowrap" @click="getLists(true)">{{renders.search.button}}</view>
					</view>
				</view>
			</view>
			<view id="types">
				<view class="types d-padding">
					<view class="types_box d-flex justify-content-between align-items-center">
						<view :class="['type text-center', type.alias == data.sections.formData.type ? 'color fw-bolder' : '']" v-for="(type, index) in renders.types.lists" :key="index" @click="switchType(type.alias)">{{type.name}}</view>
					</view>
				</view>
			</view>
			<view id="lists">
				<view class="lists d-padding">
					<view class="lists_box">
						<view class="item section" v-for="(item, index) in data.sections.orders.lists" :key="index">
							<view class="top d-flex justify-content-between align-items-center">
								<view class="order_sn vice_text">订单号: {{item.order_sn}}</view>
								<view class="status fw-bolder color">{{item.status_format}}</view>
							</view>
							<view class="main d-flex justify-content-between align-items-center" @click="navigateAction('orders.detail', item.params)">
								<view class="cover_box">
									<u--image :src="item.goods_cover" width="140rpx" height="140rpx" radius="20rpx" mode="aspectFit" :bgColor="globalData.colors.current.section" class="cover">
									  <template v-slot:loading>
										 <image src="@/static/media/images/loading.png" mode="aspectFit"></image>
									  </template>
									  <template v-slot:error>
										 <image src="@/static/media/images/loading.png" mode="aspectFit"></image>
									  </template>
									</u--image>
								</view>
								<view class="infos">
									<view class="title fw-bolder u-line-2">{{item.goods_title}}</view>
									<view class="prices d-flex justify-content-between align-items-center">
										<view class="price color fw-bolder">¥{{(item.amount/100).toFixed(2)}}</view>
										<view class="count vice_text">x {{item.count}}</view>
									</view>
								</view>
							</view>
							<view class="buttons d-flex justify-content-end align-items-center">
								<view :class="['button', button.active ? 'bg' : 'color border-color']" v-for="(button, b_index) in item.buttons" :key="b_index" @click="buttonAction(button.action, item.params)">{{button.name}}</view>
							</view>
						</view>
						<view class="order_lists_loading" v-if="data.sections.orders.mode == 'loading'">
							<u-loading-icon mode="circle" :inactive-color="globalData.colors.theme.color" :textColor="globalData.colors.theme.color" :color="globalData.colors.current.vice" size="13" textSize="13" text="正在加载列表..."></u-loading-icon>
						</view>
						<view class="order_lists_finished text-center fw-bold vice_text" v-if="data.sections.orders.mode == 'finished'">— 已到底部 —</view>
						<view class="order_lists_empty text-center" v-if="data.sections.orders.mode == 'empty'">
							<view class="iconfont icon-search font-90"></view>
							<view class="text vice_text fw-bold">暂无相关订单信息</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="shows.services" @close="shows.services = false" mode="center" :closeOnClickOverlay="true" bgColor="transparent" :customStyle="{width: '100%'}">
			<view class="domxee_popup d-padding">
				<view class="section" id="services_popup">
					<view class="popup_close" @click="shows.services = false"><view class="domxee_icon">&#xe619;</view></view>
					<view class="title fw-bolder main_text text-center">{{renders.popups.services.title}}</view>
					<view class="descriptions">
						<view class="desc text-center" v-for="(service, index) in renders.popups.services.lists" :key="index">{{service}}</view>
					</view>
				</view>
				<view class="contact d-padding" id="services_popup_contact">
					<view class="contact_button button_bg text-center fw-bold" @click="callPhone(renders.popups.services.tel)">拨打电话</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				globalData: {windowWidth: 0, statusBarHeight: 0, navHeight: 0, navigationBarHeight: 0, wechatCapsule: {width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0}, defaultThemes: [], colors: {}, configs: {} },
				scroll: 0,
				current_page: {route: '', params: {}, url: ''},
				ready: false,
				options: {

				},
				styles: {

				},
				shows: {
					services: false,
				},
				renders: {
					search: {enable: false, placeholder: '', button: ''},
					types: {enable: false, lists: [{alias: '', name: ''}]},
					popups: {services: {title: '', lists: [], tel: ''}}
				},
				data: {
					sections: {
						formData: {search_keyword: '', type: 'all', page: 1},
						orders: {mode: 'loading', lists: []}
					},
				},
			}
		},
		onReady() {						//页面渲染完毕后操作（动态获取/调整元素宽高位置等需在ready后获取最为准确）
			//设置对象
			let that = this;

		},
		onShow() {						//页面显示后操作（关闭、后台运行等操作后重新打开访问触发、可在实时性较高操作页面进行全页刷新）
			//设置对象
			let that = this;
			//判断是否准备就绪
			if (that.ready) {
				//初始数据
				that.initInformation();
			}
		},
		onLoad(options) {				//页面加载时操作（options可获取当前页面传递参数，如：options.id）
			//设置对象
			let that = this;
			//设置当前页面数据
			that.current_page = that.nii_common.pages.getCurrentUrl();
			//设置默认数据
			that.globalData = that.nii_common.pages.initGlobalData(getApp().globalData);
			//设置参数
			that.options = options;
			//判断是否指定type
			if (typeof (options.type) !== 'undefined' && options.type.length > 0) {
				//设置默认信息
				that.data.sections.formData.type = options.type;
			}
			//初始数据
			that.initInformation();
		},
		methods: {
			//默认跳转操作
			navigateAction: function(alias, requestParams = {}, action = 'navigate', authenticate = false, animationType = 'slide-in-right') {
				//跳转页面
				this.nii_common.navigate.action(alias, requestParams, action, authenticate, animationType);
			},
			callPhone: function(mobile) {
				//拨打电话
				uni.makePhoneCall({
					phoneNumber: mobile
				})
			},
			initInformation: function() {
				//设置对象
				let that = this;
				//loading
				!that.ready && that.nii_common.toastLoading('加载中');
				//开始准备
				that.ready = false;
				//发起请求
				that.nii_common.request.build('orders/information', {}, 'GET', function (res) {
					//设置准备已就绪
					that.ready = true;
					//设置数据
					that.renders = res.data;
					//获取列表
					that.getLists(true);
				}, function (res) {
					//提示错误
					that.nii_common.toastAlert(res.msg);
				}, function(res) {
					//隐藏loading
					that.nii_common.toastLoadingHide();
				}, true);	
			},
			getLists: function (refresh) {
				//设置对象
				let that = this;
				//判断是否刷新列表
				if (refresh) {					
					that.data.sections.formData.page = 1;
					that.data.sections.orders = {mode: 'refresh', lists: []};	
				}
				//判断是否无追加内容
				if (that.data.sections.orders.mode !== 'finished' && that.data.sections.orders.mode !== 'empty' && that.data.sections.orders.mode !== 'loading') {				
					//设置刷新
					that.data.sections.orders.mode = 'loading';				
					//发起请求
					that.nii_common.request.build('orders/lists', that.data.sections.formData, 'POST', function (res) {
						//设置已激活
						that.data.sections.orders.mode = 'active';
						//判断是否无数据
						if (uni.$u.test.isEmpty(res.data.lists)) {
							//判断当前页码是否为第一页
							if (parseInt(that.data.sections.formData.page) === 1) {
								//设置数据为空
								that.data.sections.orders.mode = 'empty';
							} else {
								//设置数据已加载完毕
								that.data.sections.orders.mode = 'finished';
							}
						} else {
							//循环列表
							res.data.lists.forEach((good, index) => {
								//增加信息
								that.data.sections.orders.lists.push(good);
							});
							//判断是否为最后一页
							if (parseInt(res.data.lists.length) < parseInt(res.data.page_size)) {
								//设置数据已加载完毕
								that.data.sections.orders.mode = 'finished';
							}
						}
						//增加页码
						that.data.sections.formData.page += 1;
					}, function (res) {
						//提示错误
						that.nii_common.toastAlert(res.msg);
					}, function(res) {
						//隐藏loading
						that.nii_common.toastLoadingHide();
					}, true);	
				}
			},
			switchType: function (type) {
				//设置对象
				let that = this;
				//设置类型
				that.data.sections.formData.type = type;
				//刷新列表
				that.getLists(true);
			},
			buttonAction: function (action, params) {
				//获取对象
				const that = this;
				//根据操作方式处理
				switch (action) {
					case 'pay':
					//跳转收银台
					that.navigateAction('cashier', params);
					break;
					case 'refund':
					//跳转收银台
					that.navigateAction('account.orders.refund', params);
					break;
					case 'cancel':
						//弹窗提示
						uni.showModal({
							title: '温馨提示',
							content: '请确认是否取消该订单？',
							confirmColor: that.globalData.colors.theme.color,
							confirmText: '确定',
							cancelText: '取消',
							success:function(res){
								//判断是否确认
								if (res.confirm) {
									//loading
									that.nii_common.toastLoading('正在取消...');
									//发起请求
									that.nii_common.request.build('app/orders/cancel', params, 'POST', function (res) {
										//提示成功
										that.nii_common.toastAlert('取消成功', 'success');
										//刷新列表
										that.getLists(true);
									}, function (res) {
										//提示错误
										that.nii_common.toastAlert(res.msg);
									}, function(res) {
										//隐藏loading
										that.nii_common.toastLoadingHide();
									});	
								}
							}
						});
					break;
					case 'confirm':
						//弹窗提示
						uni.showModal({
							title: '温馨提示',
							content: '请确认是否已收到货物并确认收货？',
							confirmColor: that.globalData.colors.theme.color,
							confirmText: '确定',
							cancelText: '取消',
							success:function(res){
								//判断是否确认
								if (res.confirm) {
									//loading
									that.nii_common.toastLoading('正在确认...');
									//发起请求
									that.nii_common.request.build('app/orders/confirm', params, 'POST', function (res) {
										//提示成功
										that.nii_common.toastAlert('收货成功', 'success');
										//刷新列表
										that.getLists(true);
									}, function (res) {
										//提示错误
										that.nii_common.toastAlert(res.msg);
									}, function(res) {
										//隐藏loading
										that.nii_common.toastLoadingHide();
									});	
								}
							}
						});
					break;
					default:
					//显示弹窗
					that.shows.services = true;
					break;
				}
			},
			// requestDemo: function() {
			// 	//获取对象
			// 	const that = this;
			// 	//loading
			// 	that.nii_common.toastLoading('处理中...');
			// 	//发起请求
			// 	that.nii_common.request.build('', {}, 'POST', function (res) {
			// 		//提示成功
			// 		that.nii_common.toastAlert('处理成功', 'success');
			// 	}, function (res) {
			// 		//提示错误
			// 		that.nii_common.toastAlert(res.msg);
			// 	}, function(res) {
			// 		//隐藏loading
			// 		that.nii_common.toastLoadingHide();
			// 	});	
			// }
		},
		onPullDownRefresh(res) {
			//设置对象
			let that = this;
			
			//TODO：页面下拉刷新时的事件
			
			
			//停止刷新
			uni.stopPullDownRefresh();	
		},
		onReachBottom() {
			//设置对象
			let that = this;
			//获取列表
			that.getLists();
		},
		onPageScroll: function(e) {
			//设置页面滑动高度
			this.scroll = e.scrollTop

			//TODO：需判断滑动高度后处理的事件

		}
	}

</script>

<style lang="scss">
	#header_wrapper {
		#title {
			font-size: 40rpx;
			line-height: 48rpx;
			letter-spacing: 1rpx;
			margin-left: 10rpx;
			font-weight: 600;
		}
		.domxee_icon {			
			font-size: 36rpx;
			line-height: 60rpx;
			margin-right: 20rpx;
		}
		.domxee_icon:last-child {
			margin-right: 0;
		}
	}
	#content_wrapper {
		padding-bottom: 300rpx;
		#search {
			margin-top: 30rpx;
			.search {
				.search_box {
					padding: 10rpx 10rpx 10rpx 30rpx;
					border-radius: 40rpx;
					.search_icon {
						font-size: 44rpx;
						margin-right: 10rpx;
					}
					.search_input {
						font-size: 28rpx;
						line-height: 40rpx;
						margin-right: 30rpx;
					}
					.search_button {
						border-radius: 26rpx;
						padding: 6rpx 30rpx;
						font-size: 28rpx;
						line-height: 40rpx;
					}
				}
			}
		}
		#types {
			margin-top: 30rpx;
			.types {
				.types_box {
					.type {
						width: 120rpx;
						font-size: 24rpx;
						line-height: 34rpx;
					}
				}
			}
		}
		#lists {
			margin-top: 30rpx;
			.lists {
				.lists_box {
					.order_lists_loading {
						margin-top: 30rpx;
					}
					.order_lists_finished {
						margin-top: 30rpx;
						font-size: 26rpx;
						line-height: 34rpx;
					}
					.order_lists_empty {
						margin-top: 30rpx;
						.domxee_icon {			
							font-size: 300rpx;
							line-height: 300rpx;
						}
						.text {
							margin-top: 10rpx;
							font-size: 26rpx;
							line-height: 34rpx;
						}
					}
					.item {
						margin-top: 20rpx;
						padding: 30rpx;
						border-radius: 20rpx;
						.top {
							.order_sn {
								font-size: 24rpx;
								line-height: 34rpx;
							}
							.status {
								font-size: 24rpx;
								line-height: 34rpx;
							}
						}
						.main {
							margin-top: 30rpx;
							.cover_box {
								width: 140rpx;
								height: 140rpx;
								border-radius: 20rpx;
								margin-right: 20rpx;
							}
							.infos {
								.title {
									width: 470rpx;
									height: 80rpx;
									font-size: 28rpx;
									line-height: 40rpx;
								}
								.prices {
									margin-top: 10rpx;
									.price {
										font-size: 36rpx;
										line-height: 50rpx;
									}
									.count {
										font-size: 28rpx;
										line-height: 40rpx;
									}
								}
							}
						}
						.buttons {
							margin-top: 30rpx;
							.button {
								border-radius: 20rpx;
								padding: 8rpx 24rpx;
								font-size: 24rpx;
								line-height: 34rpx;
								margin-right: 20rpx;
							}
							.button:last-child {
								margin-right: 0;
							}
						}
						
					}
					.item:first-child {
						margin-top: 0;
					}
				}
			}
		}
	}
	#services_popup {
		padding-bottom: 100rpx;
		.title {
			font-size: 36rpx;
			line-height: 50rpx;
		}
		.descriptions {
			margin-top: 80rpx;
			.desc {
				margin-top: 40rpx;
				font-size: 28rpx;
				line-height: 40rpx;
			}
			.desc:first-child {
				margin-top: 0;
			}
		}
	}
	#services_popup_contact {
		width: 580rpx;
		margin: 40rpx auto 0 auto;
		.contact_button {
			padding: 28rpx;
			font-size: 32rpx;
			line-height: 44rpx;
		}
	}
</style>
